<?php
namespace app\index\controller;


use think\Request;
use think\Db;
use Myredis\Myredis;

class Upload extends Indexcontroller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $uid=input('session.uid');
        $count=DB::table('xo_goods_up')->where(['uid'=>$uid])->count();
        $load=Myredis::get($uid.'_load');
        if(empty($load)){
            $load['up']=$count;
            $load['check']=DB::table('xo_goods_up')->where(['uid'=>$uid,'check'=>'2'])->count();
            $load['pass']=DB::table('xo_goods_up')->where(['uid'=>$uid,'check'=>'1'])->count();
            $load['down']=DB::table('xo_user_down')->where(['uid'=>$uid])->count();
            Myredis::setex($uid.'_load',600,$load);
        }

        $data=DB::table('xo_goods_up')->field(true)->where(['uid'=>$uid])->paginate(8);
        $page=$data->render();
//        $data=DB::table('xo_goods_up')->field(true)->where(['uid'=>$uid])->select();

        $down=Db::table('xo_user_down')->field('g.icon,g.sold,d.downtime,d.gid')->alias('d')->join('xo_goods_up g','d.gid=g.id','left')->select();

        $list=array();
        foreach($down as $key =>$val)
        {
            if(!empty($val['icon'])){
                $list[]=$val;
            }
        }


        if(empty($count))
        {
            return view('upload/empty',['load'=>$load,'list'=>$down]);
        }else{
            return view('upload/editing',['data'=>$data,'page'=>$page,'list'=>$list,'load'=>$load]);
        }


    }


    public function up()
    {
        $cate=Db::table('xo_admin_category')->field('display',true)->order('concat(path,id)')->select();
        foreach ($cate as $key => $val){
            //统计逗号
           $num=substr_count($val['path'],',');
           //重复空格num次
            $cate[$key]['nbsp']=str_repeat('-',($num-1)*4);
        }
        return view('upload/up',['cate'=>$cate]);
    }

    public function editing()
    {
        $name=input('session.name');
        $id=input('session.uid');

        $count=DB::table('xo_goods_up')->where(['uid'=>$id])->count();

        $load=Myredis::get($id.'_load');
        if(empty($load)){
            $load['up']=$count;
            $load['check']=DB::table('xo_goods_up')->where(['uid'=>$id,'check'=>'2'])->count();
            $load['pass']=DB::table('xo_goods_up')->where(['uid'=>$id,'check'=>'1'])->count();
            $load['down']=DB::table('xo_goods_down')->where(['uid'=>$id])->count();
            Myredis::setex($id.'_load',600,$load);
        }


        $data=DB::table('xo_goods_up')->field(true)->where(['uid'=>$id])->paginate(8,$count);
        $page=$data->render();

        $down=Db::table('xo_user_down')->field('g.icon,g.sold,d.downtime,d.gid')->alias('d')->join('xo_goods_up g','d.gid=g.id','left')->select();
        $list=array();
        foreach($down as $key =>$val)
        {
            if(!empty($val['icon'])){
                $list[]=$val;
            }
        }
        if(empty($count)){
            return view('upload/empty',['load'=>$load,'list'=>$down]);
        }else{
            return view('upload/editing',['data'=>$data,'page'=>$page,'list'=>$list,'load'=>$load]);
        }
    }

    public function waiting()
    {
        $name=input('session.name');
        $id=input('session.uid');
        $count=DB::table('xo_goods_up')->where(['uid'=>$id,'check'=>'2'])->count();

        $load=Myredis::get($id.'_load');
        if(empty($load)){
            $load['up']=$count;
            $load['check']=DB::table('xo_goods_up')->where(['uid'=>$id,'check'=>'2'])->count();
            $load['pass']=DB::table('xo_goods_up')->where(['uid'=>$id,'check'=>'1'])->count();
            $load['down']=DB::table('xo_user_down')->where(['uid'=>$id])->count();
            Myredis::setex($id.'_load',600,$load);
        }

        $data=DB::table('xo_goods_up')->field(true)->where(['uid'=>$id,'check'=>'2'])->order('addtime','desc')->paginate(8,$count);
        $page=$data->render();
        $data=$data->items();
        $down=Db::table('xo_user_down')->field('g.icon,g.sold,d.downtime,d.gid')->alias('d')->join('xo_goods_up g','d.gid=g.id','left')->select();

        $list=array();
        foreach($down as $key =>$val)
        {
            if(!empty($val['icon'])){
                $list[]=$val;
            }
        }
        if(empty($count)){
            return view('upload/empty',['load'=>$load,'list'=>$down]);
        }else{
            return view('upload/waiting',['data'=>$data,'page'=>$page,'list'=>$list,'load'=>$load]);
        }
    }

    public function passed()
    {
        $name=input('session.name');
        $id=input('session.uid');
        $count=DB::table('xo_goods_up')->where(['uid'=>$id,'check'=>'1'])->count();

        $load=Myredis::get($id.'_load');
        if(empty($load)){
            $load['up']=$count;
            $load['check']=DB::table('xo_goods_up')->where(['uid'>$id,'check'=>'2'])->count();
            $load['pass']=DB::table('xo_goods_up')->where(['uid'=>$id,'check'=>'1'])->count();
            $load['down']=DB::table('xo_user_down')->where(['uid'=>$id])->count();
            Myredis::setex($id.'_load',600,$load);
        }


        $data=DB::table('xo_goods_up')->field(true)->where(['uid'=>$id,'check'=>1])->order('addtime','desc')->paginate(8,$count);
        $page=$data->render();
        $data=$data->items();
        $down=Db::table('xo_user_down')->field('g.icon,g.sold,d.downtime,d.gid')->alias('d')->join('xo_goods_up g','d.gid=g.id','left')->select();
        $list=array();
        foreach($down as $key =>$val)
        {
            if(!empty($val['icon'])){
                $list[]=$val;
            }
        }
        if(empty($count)){
            return view('upload/empty',['load'=>$load,'list'=>$down]);
        }else{
            return view('upload/passed',['data'=>$data,'page'=>$page,'list'=>$list,'load'=>$load]);
        }
    }



    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        if(!$request->isPost()){
            return redirect('index/error/index');
        }
        if(!input('?session.uid')
        ){
            return redirect('index/index/index');
        }

        $desc=input('post.desc');
        $uid=input('session.uid');
        $cid=input('post.cate');
        $this->upload($desc,$uid,$cid);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }



    public function doedit()
    {
        $id=input('post.id');

        $oldname=input('post.oldname');
        $newname=input('post.newname');

        if($oldname==$newname){
            //说明原来名字和现在名字一样
            return $this->success('和原来名字一样哦');
        }else{
            $info['pname']=$newname;
            $res=Db::table('xo_goods_up')->where(['id'=>$id])->update($info);
            if($res>0){
                $this->success('修改成功');
            }else{
                $this->success('修改失败,请稍后再试');
            }
        }
    }




    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if(!Request::instance()->isAjax()){
            return $this->error('非法请求',url('index/index/index'));
        }
        $res=Db::table('xo_goods_up')->where(['id'=>$id])->delete();

        if($res>0){
            $data['status']=true;
        }else{
            $data['status']=false;
        }
        return json($data);

    }
}
